/**
 * Created by zhanglihe on 15/11/19.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'calendar'
], function ($, common, ready, Collection, template, Model, dialog, calendar) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            var site_withdraw_id = getUrlPara('site_withdraw_id');
            if (site_withdraw_id) {
                $('input[name="start_time"]').val('');
                $('input[name="end_time"]').val('');
            }
            this.collection = new Collection({
                url: _global.url.api + 'order/counts',
                condition: {
                    limit: 10,
                    offset: 0,
                    site_withdraw_id: site_withdraw_id,
                    start_time: $('input[name="start_time"]').val(),
                    end_time: $('input[name="end_time"]').val(),
                },
                model: {
                    event: {
                        '.rebate_list:click': 'rebate_list',
                        '.commission_list:click': 'commission_list'
                    },
                    rebate_list: function (e) {
                        e.preventDefault();
                        if (this.data.order_id < 114324) {
                            dialog({
                                'content': '请联系项目经理查看',
                                ok: function () {
                                }
                            }).show(e.target);
                            return false;
                        }
                        var html = template('rebate_list-tpl');
                        var dialogObj = dialog({
                            title: '订单(' + this.data.sn + ')分享家分成明细(总分成:' + this.data.rebate_amount + ')',
                            content: html,
                            width: '800',
                            height: '400',
                            modal: true,
                            ok: function () {
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show();
                        dialogObj._$('content').css('overflow', 'auto');
                        show_rebate_list(this.data);
                    },
                    commission_list: function (e) {
                        e.preventDefault();
                        var html = template('commission_list-tpl');
                        var dialogObj = dialog({
                            title: '订单(' + this.data.sn + ')平台分成明细(总分成:' + this.data.commission_amount + ')',
                            content: html,
                            width: '800',
                            height: '400',
                            modal: true,
                            ok: function () {
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show();
                        dialogObj._$('content').css('overflow', 'auto');
                        show_commission_list(this.data);
                    }
                },
                callback: function () {
                    loaded();
                    $('.pagination').append('<li><a href="javascript:">记录总金额：￥' + _this.collection.result._amount + '</a></li>');
                }
            });
            this.collection.fetch();
        },
        event: function () {
            var _this = this;
            //日期
            $('.form_datetime').calendar();
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                click_search();
            });

            var click_search = function (fun) {
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.condition.start_time = $('input[name="start_time"]').val();
                _this.collection.condition.end_time = $('input[name="end_time"]').val();
                _this.collection.callback = function () {
                    if (fun && fun == 'toExcel') {
                        toExcel();
                        _this.collection.callback = function () {
                        }
                    }
                }
                _this.collection.reload();
            };
            /**
             * 按状态查询
             */
            var start_time = '', end_time = '';
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                var status = $(e.target).attr('rel');
                _this.collection.condition.isSubsidy = 0;
                if (status == 'subsidy') {
                    _this.collection.condition.isSubsidy = 1;
                    if (_this.collection.condition.start_time) {
                        start_time = _this.collection.condition.start_time;
                    }
                    if (_this.collection.condition.end_time) {
                        end_time = _this.collection.condition.end_time;
                    }
                    _this.collection.condition.start_time = $('input[name="start_time"]').val('').val();
                    _this.collection.condition.end_time = $('input[name="end_time"]').val('').val();
                } else {
                    _this.collection.condition.start_time = $('input[name="start_time"]').val(start_time).val();
                    _this.collection.condition.end_time = $('input[name="end_time"]').val(end_time).val();
                }
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });

//to excel
            var href;
            var toExcelWindow;
            $(".btn-excel").on("click", function (e) {
                e.preventDefault();
                href = $(this).attr('href');
                toExcelWindow = dialog({
                    content: '请稍等 ,正在导出...',
                    width: 200,
                    modal: true
                }).show();
                click_search('toExcel');
            });

            var toExcel = function () {
                toExcelWindow.close();
                console.log(_this.collection.result._count);
                var limit = 1000;
                var params = '';
                var data = _this.collection.condition;
                var total = _this.collection.result._count;

                if (total < 1) {
                    alert('没有需要导出的列表');
                    return false;
                }

                for (var i in data) {
                    if (i == 'limit' || i == 'offset') continue;
                    params = params + '&' + i + '=' + encodeURIComponent(data[i]);
                }
                href = (href.indexOf('?') === -1) ? href + '?' : href;
                href = href + params + '&limit=' + limit + '&offset=';

                if (total > limit) {
                    var showData = {};
                    showData.pages = Math.ceil(total / limit);
                    showData.total = total;
                    showData.limit = limit;
                    showData.href = href;
                    dialog({
                        title: '导出列表Excel文件',
                        content: template('to-excel-tpl', showData),
                        width: 600,
                        heigth: 500,
                        modal: true,
                        ok: function () {
                        },
                        cancel: function () {
                        }
                    }).show();
                } else {
                    window.location.href = href;
                }
            }
        }
    };
    var show_rebate_list = function (order) {
        var collection = new Collection({
            container: 'data-list_rebate',
            template: 'rebate_data-tpl',
            url: _global.url.api + 'order/rebates',
            show_page: false,
            condition: {limit: 1000, offset: 0, order_id: order.order_id},
            model: {},
            callback: function () {
                loaded();
            }
        });
        collection.fetch();
    }
    var show_commission_list = function (order) {
        var collection = new Collection({
            container: 'data-list_commission',
            template: 'commission_data-tpl',
            url: _global.url.api + 'order/commissions',
            show_page: false,
            condition: {limit: 1000, offset: 0, order_id: order.order_id},
            model: {},
            callback: function () {
                loaded();
            }
        });
        collection.fetch();
    }

    new index();

});